package com.yh.homestay.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.common.StaffEditResult;
import com.yh.homestay.dao.UserStaffLikeMapper;
import com.yh.homestay.dao.UserStaffMapper;
import com.yh.homestay.pojo.UserStaff;
import com.yh.homestay.pojo.UserStaffExample;
import com.yh.homestay.service.UserStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserStaffServiceImpl implements UserStaffService {
    @Autowired
    private UserStaffMapper userStaffMapper;
    @Autowired
    private UserStaffLikeMapper staffMapper;

    @Override
    public HomeStayResult selectAll(String staffName, Integer page, Integer limit) {
        Map map=new HashMap();
        map.put("staffName",staffName);
        UserStaffExample userStaffExample = new UserStaffExample();
        UserStaffExample.Criteria criteria = userStaffExample.createCriteria();
        PageHelper.startPage(page,limit);
        List<UserStaff> staff = staffMapper.findByName(map);
        PageInfo pageInfo = new PageInfo(staff);
        HomeStayResult res=new HomeStayResult(0," ", (int) pageInfo.getTotal(),pageInfo.getList());
        return res;
    }

    @Override
    public int addStaff(UserStaff userStaff) {
        int insert = userStaffMapper.insert(userStaff);
        return insert;
    }

    @Override
    public HomeStayResult delete(Integer id) {
            UserStaff userStaff = new UserStaff();
            userStaff.setStaffId(id);
            int delete = userStaffMapper.delete(userStaff);
            if(delete>0){
                return new HomeStayResult(200,"ok",0,null);
            }else {
                return new HomeStayResult(500,"error",0,null);
            }
    }

    @Override
    public HomeStayResult update(Integer id, String staffName, String staffPhone, String staffHiredate, Double staffSalary, Double staffComm, Integer staffMgr) {
        UserStaff userStaff = new UserStaff();
        //   userOwner.setOwnerId(id);
        userStaff.setStaffName(staffName);
        userStaff.setStaffPhone(staffPhone);
        userStaff.setStaffHiredate(staffHiredate);
        userStaff.setStaffSalary(staffSalary);
        userStaff.setStaffComm(staffComm);
        userStaff.setStaffMgr(staffMgr);
        UserStaffExample example = new UserStaffExample();
        UserStaffExample.Criteria criteria = example.createCriteria();
        criteria.andStaffIdEqualTo(id);
        int update = userStaffMapper.updateByExampleSelective(userStaff,example);
        if(update>0){
            return new HomeStayResult(200,"ok",0,null);
        }else {
            return new HomeStayResult(500,"error",0,null);
        }
    }

    @Override
    public StaffEditResult selectOne(Integer id) {
        UserStaffExample userStaffExample = new UserStaffExample();
        UserStaffExample.Criteria criteria = userStaffExample.createCriteria();
        criteria.andStaffIdEqualTo(id);
        List<UserStaff> userStaffs = userStaffMapper.selectByExample(userStaffExample);
        if(userStaffs!=null&&userStaffs.size()>0){
            return new StaffEditResult(200,"ok",userStaffs.get(0));
        }else{
            return new StaffEditResult(500,"err",null);
        }
    }
@Override
public HomeStayResult delectAll(List<Integer> ids) {
    UserStaffExample example = new UserStaffExample();
    UserStaffExample.Criteria criteria = example.createCriteria();
    criteria.andStaffIdIn(ids);
    int delete = userStaffMapper.deleteByExample(example);
    if (delete > 0) {
        return new HomeStayResult(200, "ok",0,null);
    } else {
        return new HomeStayResult(500, "error",0,null);
    }


}
}

